package com.dsbj.apolo.facecollector.controller;


import com.dsbj.apolo.facecollector.entity.CollectInfo;
import com.dsbj.apolo.facecollector.service.FaceCollectService;
import com.dsbj.apolo.facecollector.utils.ExcelUtils;
import com.dsbj.common.msg.ResponseMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("face")
public class FaceController extends BaseController{

    @Value(value = "${gateway-path}")
    private String gatewayPath;

    @ModelAttribute
    protected void attributes(HttpServletRequest request, ModelMap model)
    {
        String base = request.getContextPath()+gatewayPath;
        model.addAttribute("ctx", base);
        model.addAttribute("static", base + "/static");
        model.addAttribute("system_js", base + "/static/system/js");
        model.addAttribute("system_css", base + "/static/system/css");
        model.addAttribute("system_images", base + "/static/system/images");
        model.addAttribute("plugins", base + "/static/plugins");

        model.addAttribute("cssVersion", "1.34");

    }


    @Autowired
    private FaceCollectService faceCollectService;

    @RequestMapping("/collect")
    public String collect(Model model, HttpServletRequest request, HttpServletResponse response){

        return "collect";
    }

    @RequestMapping("/uploadExcel")
    public String uploadExcel(Model model, HttpServletRequest request, HttpServletResponse response){

        return "uploadExcel";
    }

    @RequestMapping("/saveExcel")
    @ResponseBody
    public ResponseMessage saveExcel(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam("file") MultipartFile file) throws IOException {
        String name=file.getOriginalFilename();
        if(name.length()<6|| !name.substring(name.length()-5).equals(".xlsx")){
            return ResponseMessage.error("文件格式错误");
        }

        faceCollectService.processExcel(file);

        return ResponseMessage.ok();
    }

    @RequestMapping(method = RequestMethod.POST,value = "/saveCollect")
    @ResponseBody
    public ResponseMessage saveNew(Model model, HttpServletRequest request, HttpServletResponse response, CollectInfo collectInfo){

        System.out.println("开始提交");

        return faceCollectService.processCollectInfo(collectInfo);
    }


    @RequestMapping("/getCollectInfoByIdNo")
    @ResponseBody
    public ResponseMessage getCollectInfoByIdNo(Model model, HttpServletRequest request, HttpServletResponse response,String idNo){

        return faceCollectService.getCollectInfoByIdNo(idNo);
    }

    @RequestMapping("/download")
    public String download(Model model, HttpServletRequest request, HttpServletResponse response,String idNo){

        return "download";
    }

    @RequestMapping("/downloadZip")
    public void downloadZip(Model model, HttpServletRequest request, HttpServletResponse response,String idNo){

        //faceCollectService.downloadFaces(response);

    }

    @RequestMapping("/collect-info")
    public String collectIn(Model model, HttpServletRequest request, HttpServletResponse response,String city,String company,String companyName){

        if(StringUtils.isBlank(city)){
            model.addAttribute("city","苏州");
            model.addAttribute("company","01-02");
            model.addAttribute("companyName","苏州东山精密");
        }else{
            if(city.equals("yc")){
                model.addAttribute("city","盐城");
            }else{
                model.addAttribute("city","苏州");
            }
            model.addAttribute("company",company);
            model.addAttribute("companyName",companyName);
        }


        return "questionnaire/add";
    }


}
